using Senparc.Weixin.MP.Helpers;
using System;
using System.Collections;
using System.Text;
using System.Web;
namespace Senparc.Weixin.MP.TenPayLib
{
	public class ClientRequestHandler
	{
		private string GateUrl;
		private string Key;
		protected Hashtable Parameters;
		private string DebugInfo;
		protected HttpContext HttpContext;
		public ClientRequestHandler(HttpContext httpContext)
		{
			this.Parameters = new Hashtable();
			this.HttpContext = (httpContext ?? HttpContext.Current);
		}
		public virtual void Init()
		{
		}
		public string GetGateUrl()
		{
			return this.GateUrl;
		}
		public void SetGateUrl(string gateUrl)
		{
			this.GateUrl = gateUrl;
		}
		public string GetKey()
		{
			return this.Key;
		}
		public void SetKey(string key)
		{
			this.Key = key;
		}
		public virtual string GetRequestURL()
		{
			this.CreateSign();
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "key".CompareTo(text) != 0 && "spbill_create_ip".CompareTo(text) != 0)
				{
					stringBuilder.Append(text + "=" + TenPayUtil.UrlEncode(text2, this.GetCharset()) + "&");
				}
				else
				{
					if ("spbill_create_ip".CompareTo(text) == 0)
					{
						stringBuilder.Append(text + "=" + text2.Replace(".", "%2E") + "&");
					}
				}
			}
			if (stringBuilder.Length > 0)
			{
				stringBuilder.Remove(stringBuilder.Length - 1, 1);
			}
			return this.GetGateUrl() + "?" + stringBuilder.ToString();
		}
		protected virtual void CreateSign()
		{
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "".CompareTo(text2) != 0 && "sign".CompareTo(text) != 0 && "key".CompareTo(text) != 0)
				{
					stringBuilder.Append(text + "=" + text2 + "&");
				}
			}
			stringBuilder.Append("key=" + this.GetKey());
			string text3 = MD5UtilHelper.GetMD5(stringBuilder.ToString(), this.GetCharset()).ToLower();
			this.SetParameter("sign", text3);
			this.SetDebugInfo(stringBuilder.ToString() + " &sign=" + text3);
		}
		public string GetParameter(string parameter)
		{
			string text = (string)this.Parameters[parameter];
			return (text == null) ? "" : text;
		}
		public void SetParameter(string parameter, string parameterValue)
		{
			if (parameter != null && parameter != "")
			{
				if (this.Parameters.Contains(parameter))
				{
					this.Parameters.Remove(parameter);
				}
				this.Parameters.Add(parameter, parameterValue);
			}
		}
		public void DoSend()
		{
			this.HttpContext.Response.Redirect(this.GetRequestURL());
		}
		public string GetDebugInfo()
		{
			return this.DebugInfo;
		}
		public void SetDebugInfo(string debugInfo)
		{
			this.DebugInfo = debugInfo;
		}
		public Hashtable GetAllParameters()
		{
			return this.Parameters;
		}
		protected virtual string GetCharset()
		{
			return this.HttpContext.Request.ContentEncoding.BodyName;
		}
	}
}
